<?php
require_once(dirname(__FILE__).'/../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('base.wplog');

class TestWpMySqlLog extends WpTestSuite
{
  private $_host = 'localhost';
  private $_user = 'wp';
  private $_password = 'wp';
  private $_db = 'wp';
  private $_table = 'log_log';
  
  /**
   * Preparation
   */
  public function prepare()
  {
    $this->addMessage('Using database host: '.$this->_host);
    $this->addMessage('Using database: '.$this->_db);
    $this->addMessage('Using user: '.$this->_user);
    $this->addMessage('Using database table: '.$this->_table);
    
    $this->addMessage('Connect to database...');
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      die;
    }
    $this->addMessage('... established!');
    
    $this->addMessage('Has database?');
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('Database not found. Try creating...');
      //mysql_select_db('information_schema', $c);
      if (!mysql_query('CREATE DATABASE wp')) {
        $this->addMessage('... not successfull. Test stopped.');
        die;
      } else {
        $this->addMessage('... successfull.');
        mysql_select_db($this->_db, $c);    
      }
    } else {
      $this->addMessage('Database found'); 
    }
    
    $r = mysql_query('SHOW TABLES');
    $found = false;
    while ($row = mysql_fetch_array($r)) {
      if ($row[0] === $this->_table) {
        $found=true; break;
      }
    }
    mysql_free_result($result);
    
    if (!$found) {
      if (!mysql_query('CREATE TABLE  `log_log` ('.
        '`type` varchar(255) NOT NULL,'.
        '`location` varchar(255) NOT NULL,'.
        '`user` varchar(255) NOT NULL,'.
        '`datetime` varchar(50) NOT NULL,'.
        '`title` varchar(255) NOT NULL,'.
        '`body` text NOT NULL,'.
        '`number` varchar(50) NOT NULL,'.
        '`level` int(11) NOT NULL'.
        ') ENGINE=MyISAM DEFAULT CHARSET=latin1'
      )) {
        $this->addMessage('Table not found. Try to create ... failed. Test stopped.');
        die;
      } else {
        $this->addMessage('Table not found. Try to create ... successfull.');
      }
    } else {
      $this->addMessage('Table found.');
    }
    
    if (!mysql_query('TRUNCATE '.$this->_table)) {
      $this->addMessage('Trying to empty the table ... failed. Test stopped.');
      die;
    }
    
    $this->addMessage('Trying to empty the table ... successfull.');
    return true;
  }
  
  /**
   * Test
   */
  public function test()
  {
    
    //-- Realtime tests --
    $l = new WpMySqlLog($this->_host, $this->_user, $this->_password, $this->_db, $this->_table, true);
    $l->logError('Location1', 'Admin', 'Test1', 'Body1', '1', 1);
    $l->logError('Location2', 'Admin', 'Test2', 'Body2', '1', 1);
    $l->logMessage('Location3', 'Admin', 'Message1', 'BodyMessage', '1', 1);
    $l->logAction('Location4', 'Admin', 'Action1', 'BodyAction', '1', 1);
    $l->logAudit('Location5', 'Admin', 'Audit1', 'BodyAudit', '1');
    
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      die;
    }
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('DB selection failed. Test stopped.');
      die;
    }
    $r = mysql_query('SELECT * FROM '.$this->_table.' ORDER BY location ASC');
    if (!$r) {
      $this->addMessage('Invalid result returned. Test stopped.');
      die;
    }
    
    $a = array();
    while ($row = mysql_fetch_assoc($r)) {
      $a[] = $row;
    }
    mysql_free_result($result);
    
    $this->addResult('Test array', true, is_array($a), '===');
    $this->addResult('Test count entries', 5, $a, 'count');
    $this->addResult('Test location1', 'Location1', $a[0]['location'], '===');
    $this->addResult('Test location2', 'Location2', $a[1]['location'], '===');
    $this->addResult('Test location3', 'Location3', $a[2]['location'], '===');
    $this->addResult('Test location4', 'Location4', $a[3]['location'], '===');
    $this->addResult('Test location5', 'Location5', $a[4]['location'], '===');
    $this->addResult('Test type1', 'error', $a[0]['type'], '===');
    $this->addResult('Test type2', 'error', $a[1]['type'], '===');
    $this->addResult('Test type3', 'message', $a[2]['type'], '===');
    $this->addResult('Test type4', 'action', $a[3]['type'], '===');
    $this->addResult('Test type5', 'audit', $a[4]['type'], '===');
    $this->addResult('Test user1', 'Admin', $a[0]['user'], '===');
    $this->addResult('Test user2', 'Admin', $a[1]['user'], '===');
    $this->addResult('Test user3', 'Admin', $a[2]['user'], '===');
    $this->addResult('Test user4', 'Admin', $a[3]['user'], '===');
    $this->addResult('Test user5', 'Admin', $a[4]['user'], '===');
    $this->addResult('Test title1', 'Test1', $a[0]['title'], '===');
    $this->addResult('Test title2', 'Test2', $a[1]['title'], '===');
    $this->addResult('Test title3', 'Message1', $a[2]['title'], '===');
    $this->addResult('Test title4', 'Action1', $a[3]['title'], '===');
    $this->addResult('Test title5', 'Audit1', $a[4]['title'], '===');
    $this->addResult('Test body1', 'Body1', $a[0]['body'], '===');
    $this->addResult('Test body2', 'Body2', $a[1]['body'], '===');
    $this->addResult('Test body3', 'BodyMessage', $a[2]['body'], '===');
    $this->addResult('Test body4', 'BodyAction', $a[3]['body'], '===');
    $this->addResult('Test body5', 'BodyAudit', $a[4]['body'], '===');
    $this->addResult('Test number1', '1', $a[0]['number'], '===');
    $this->addResult('Test number2', '1', $a[1]['number'], '===');
    $this->addResult('Test number3', '1', $a[2]['number'], '===');
    $this->addResult('Test number4', '1', $a[3]['number'], '===');
    $this->addResult('Test number5', '1', $a[4]['number'], '===');
    
    //-- Cache tests --
    if (!mysql_query('TRUNCATE '.$this->_table)) {
      $this->addMessage('An error occurred truncating the table. Test serie stopped.');
    } else {
      $this->addMessage('Truncated table for cache tests.');
    }
    
    $l = new WpMySqlLog($this->_host, $this->_user, $this->_password, $this->_db, $this->_table, false);
    $l->logError('Location1', 'Admin', 'Test1', 'Body1', '1', 1);
    $l->logError('Location2', 'Admin', 'Test2', 'Body2', '1', 1);
    $l->logMessage('Location3', 'Admin', 'Message1', 'BodyMessage', '1', 1);
    $l->logAction('Location4', 'Admin', 'Action1', 'BodyAction', '1', 1);
    $l->logAudit('Location5', 'Admin', 'Audit1', 'BodyAudit', '1');
    
    $r = mysql_query('SELECT * FROM '.$this->_table.' ORDER BY location ASC');
    if (!$r) {
      $this->addMessage('Invalid result returned. Test stopped.');
      die;
    }
    
    $a = array();
    while ($row = mysql_fetch_assoc($r)) {
      $a[] = $row;
    }
    mysql_free_result($result);
    $this->addResult('Is array', true, is_array($a), '===');
    $this->addResult('Is array empty', 0, $a, 'count');
    $this->addMessage('Destruct the instance to write cache to db.');
    $l->__destruct();
    
    $r = mysql_query('SELECT * FROM '.$this->_table.' ORDER BY location ASC');
    if (!$r) {
      $this->addMessage('Invalid result returned. Test stopped.');
      die;
    }
    
    $a = array();
    while ($row = mysql_fetch_assoc($r)) {
      $a[] = $row;
    }
    mysql_free_result($result);
    
    $this->addResult('Test array', true, is_array($a), '===');
    $this->addResult('Test count entries', 5, $a, 'count');
    $this->addResult('Test location1', 'Location1', $a[0]['location'], '===');
    $this->addResult('Test location2', 'Location2', $a[1]['location'], '===');
    $this->addResult('Test location3', 'Location3', $a[2]['location'], '===');
    $this->addResult('Test location4', 'Location4', $a[3]['location'], '===');
    $this->addResult('Test location5', 'Location5', $a[4]['location'], '===');
    $this->addResult('Test type1', 'error', $a[0]['type'], '===');
    $this->addResult('Test type2', 'error', $a[1]['type'], '===');
    $this->addResult('Test type3', 'message', $a[2]['type'], '===');
    $this->addResult('Test type4', 'action', $a[3]['type'], '===');
    $this->addResult('Test type5', 'audit', $a[4]['type'], '===');
    $this->addResult('Test user1', 'Admin', $a[0]['user'], '===');
    $this->addResult('Test user2', 'Admin', $a[1]['user'], '===');
    $this->addResult('Test user3', 'Admin', $a[2]['user'], '===');
    $this->addResult('Test user4', 'Admin', $a[3]['user'], '===');
    $this->addResult('Test user5', 'Admin', $a[4]['user'], '===');
    $this->addResult('Test title1', 'Test1', $a[0]['title'], '===');
    $this->addResult('Test title2', 'Test2', $a[1]['title'], '===');
    $this->addResult('Test title3', 'Message1', $a[2]['title'], '===');
    $this->addResult('Test title4', 'Action1', $a[3]['title'], '===');
    $this->addResult('Test title5', 'Audit1', $a[4]['title'], '===');
    $this->addResult('Test body1', 'Body1', $a[0]['body'], '===');
    $this->addResult('Test body2', 'Body2', $a[1]['body'], '===');
    $this->addResult('Test body3', 'BodyMessage', $a[2]['body'], '===');
    $this->addResult('Test body4', 'BodyAction', $a[3]['body'], '===');
    $this->addResult('Test body5', 'BodyAudit', $a[4]['body'], '===');
    $this->addResult('Test number1', '1', $a[0]['number'], '===');
    $this->addResult('Test number2', '1', $a[1]['number'], '===');
    $this->addResult('Test number3', '1', $a[2]['number'], '===');
    $this->addResult('Test number4', '1', $a[3]['number'], '===');
    $this->addResult('Test number5', '1', $a[4]['number'], '===');
  }
  
  /**
   * Cleanup
   */
  public function cleanup()
  {
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      return;
    }
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('DB selection failed. Test stopped.');
      return;
    }
    $r = mysql_query('TRUNCATE '.$this->_table);
    if (!$r) {
      $this->addMessage('Table could not be truncated.');
      return;
    }
    
    $this->addMessage('Successfully truncated table.');
  }
  
  
}

$t = new TestWpMySqlLog();
$t->start('WpMySqlLog');
